package com.zelin.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zelin.pojo.R;
import com.zelin.pojo.Student;
import com.zelin.service.StudentService;
import com.zelin.vo.StudentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("student")
public class StudentController {
    @Autowired
    private StudentService studentService;

    //查询所有
    @GetMapping("/findAll")
    public List<Student> findAll(){
        return studentService.findAll();
    }
    //分页查询
    @GetMapping("findByPage/{page}/{pageSize}")
    public R findByPage(@PathVariable int page,@PathVariable int pageSize){
        //得到分页结果
        Page<Student> studentPage = studentService.findByPage(page,pageSize);
        //返回
        return R.ok()
                .data("total",studentPage.getTotal())
                .data("rows",studentPage.getRecords());
    }
    //添加学生
    @PutMapping("/update/{sid}")
    public R update(@PathVariable int sid, @RequestBody Student student){
        //设置主键
        student.setSid(sid);
        //修改学生
        studentService.update(student);
        //返回
        return R.ok().data("msg","修改成功");
    }
    //删除学生
    @DeleteMapping("/{sid}")
    public R delete(@PathVariable int sid){
        //从数据库中删除学生
        studentService.delete(sid);
        //返回
        return R.ok().data("msg","删除成功");
    }
    //条件分页查询
    @PostMapping("/search/{page}/{pageSize}")
    public R search(@PathVariable int page,@PathVariable int pageSize,@RequestBody StudentVo student){
        //6.1 开始查询
        Page<Student> studentPage = studentService.search(page,pageSize,student);
        //6.2 返回
        return R.ok().data("total",studentPage.getTotal()).data("rows",studentPage.getRecords());
    }
}

